<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="理财产品名称" prop="financeProductsName" label-width="80">
        <el-input v-model="queryParams.financeProductsName" placeholder="请输入理财产品名称" clearable
                  @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="理财类型" prop="financeType">
        <el-select v-model="queryParams.financeType" placeholder="请选择理财类型" clearable>
          <el-option v-for="dict in dict.type.inv_finance_type" :key="dict.value" :label="dict.label"
                     :value="dict.value"/>
        </el-select>
      </el-form-item>
      <el-form-item label="合作机构" prop="cooperativeAgency">
        <el-input v-model="queryParams.cooperativeAgency" placeholder="请输入合作机构" clearable
                  @keyup.enter.native="handleQuery" @click.native="showSelect" id="cooperativeAgency"/>
<!--        <el-input type="text" ts-selected="1" id="cooperativeAgencyN" name="cooperativeAgencyN" class="layui-input" />
        <input type="hidden" id="cooperativeAgency" name="cooperativeAgency" class="layui-input">-->
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini"
                   @click="handleAdd" v-hasPermi="['loan:financeProducts:add']">新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single"
                   @click="handleUpdate" v-hasPermi="['loan:financeProducts:edit']">修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple"
                   @click="handleDelete" v-hasPermi="['loan:financeProducts:remove']">删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini"
                   @click="handleExport" v-hasPermi="['loan:financeProducts:export']">导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="financeProductsList" @selection-change="handleSelectionChange" border height="calc(100vh - 280px)">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="理财产品名称" align="center" prop="financeProductsName" width="200" :show-overflow-tooltip="true"/>
      <el-table-column label="理财类型" align="center" prop="financeType" width="150" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.inv_finance_type" :value="scope.row.financeType"/>
        </template>
      </el-table-column>
      <el-table-column label="自动赎回" align="center" prop="autoRedemption">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.autoRedemption"/>
        </template>
      </el-table-column>
      <el-table-column label="赎回规则(T+n)" align="center" prop="redemptionRule"/>
      <el-table-column label="固定期限(自然日)" align="center" prop="fixedTerm"/>
      <el-table-column label="是否保本" align="center" prop="breakEvenFlag">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.breakEvenFlag"/>
        </template>
      </el-table-column>
      <el-table-column label="收益类型" align="center" prop="fixedFlag">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.inv_fixed_flag" :value="scope.row.fixedFlag"/>
        </template>
      </el-table-column>
      <el-table-column label="预期年化收益率" align="center" prop="annualYield"/>
      <el-table-column label="收益率说明" align="center" prop="yieldDescription"/>
      <el-table-column label="合作机构" align="center" prop="cooperativeAgencyN" width="200" :show-overflow-tooltip="true"/>
<!--      <el-table-column label="启用" align="center" prop="enableFlag">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.enableFlag"/>
        </template>
      </el-table-column>-->
      <el-table-column label="申购状态" align="center" prop="purchaseStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.inv_product_purchase_status" :value="scope.row.purchaseStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="120">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit"
                     @click="handleUpdate(scope.row)" v-hasPermi="['loan:financeProducts:edit']">修改
          </el-button>
          <el-button size="mini" type="text" icon="el-icon-delete"
                     @click="handleDelete(scope.row)" v-hasPermi="['loan:financeProducts:remove']">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
                @pagination="getList"/>

    <!-- 添加或修改理财产品定义对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="理财产品名称" prop="financeProductsName">
              <el-input v-model="form.financeProductsName" placeholder="请输入理财产品名称" style="width: 85%"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="理财类型" prop="financeType">
              <el-select v-model="form.financeType" placeholder="请选择理财类型">
                <el-option v-for="dict in dict.type.inv_finance_type" :key="dict.value" :label="dict.label"
                           :value="dict.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="自动赎回">
              <el-radio-group v-model="form.autoRedemption">
                <el-radio v-for="dict in dict.type.sys_yes_no" :key="dict.value" :label="dict.value">{{dict.label}}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="赎回规则(T+n)" prop="redemptionRule">
              <el-input-number :min="0" v-model="form.redemptionRule" placeholder="请输入赎回规则(T+n)" style="width: 85%" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="固定期限(自然日)" prop="fixedTerm">
              <el-input-number :min="0" v-model="form.fixedTerm" placeholder="请输入固定期限(自然日)" style="width: 85%" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="是否保本">
              <el-radio-group v-model="form.breakEvenFlag">
                <el-radio v-for="dict in dict.type.sys_yes_no" :key="dict.value" :label="dict.value">{{ dict.label }}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="收益类型" prop="fixedFlag">
              <el-select v-model="form.fixedFlag" placeholder="请输入收益类型">
                <el-option v-for="dict in dict.type.inv_fixed_flag" :key="dict.value" :label="dict.label"
                           :value="dict.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="年化收益率" prop="annualYield">
              <el-input-number :min="0" v-model="form.annualYield" placeholder="请输入年化收益率" style="width: 85%"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="合作机构" prop="cooperativeAgency">
              <el-input v-model="form.cooperativeAgency" placeholder="请输入合作机构" style="width: 85%"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="申购状态" prop="purchaseStatus">
              <el-select v-model="form.purchaseStatus" placeholder="请选择申购状态">
                <el-option v-for="dict in dict.type.inv_product_purchase_status" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
<!--        <el-form-item label="启用">
          <el-radio-group v-model="form.enableFlag">
            <el-radio v-for="dict in dict.type.sys_yes_no" :key="dict.value" :label="dict.value">{{ dict.label }}</el-radio>
          </el-radio-group>
        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listFinanceProducts,
  getFinanceProducts,
  delFinanceProducts,
  addFinanceProducts,
  updateFinanceProducts
} from "@/api/loan/financeProducts";

export default {
  name: "FinanceProducts",
  dicts: ['inv_finance_type', 'sys_yes_no','inv_product_purchase_status','inv_fixed_flag'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 理财产品定义表格数据
      financeProductsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        financeProductsName: null,
        financeType: null,
        breakEvenFlag: null,
        fixedFlag: null,
        bpId: null,
        bankAccountId: null,
        annualYield: null,
        yieldDescription: null,
        enableFlag: null,
        purchaseStatus: null,
        autoRedemption: null,
        redemptionRule: null,
        fixedTerm: null,
        cooperativeAgency: null,
        documentType: null,
        documentCategory: null,
        businessType: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        financeProductsName: [
          {required: true, message: "理财产品名称不能为空", trigger: "blur"}
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    showSelect(){
      layui.use(['tableSelect'], function () {
        var form = layui.form,$ = layui.$,tableSelect = layui.tableSelect;

        tableSelect.render({
          elem: '#cooperativeAgency',//定义输入框input对象 必填
          checkedKey: 'cooperativeAgency',//表格的唯一建值，非常重要，影响到选中状态 必填
          searchKey: 'keyword',	//搜索输入框的name值 默认keyword
          searchPlaceholder: '关键词搜索',//搜索输入框的提示文字 默认关键词搜索
          table: {
            url: '/EquipmentManage/Equipment/GetJson',
            cols: [[
              { type: 'radio' },
              { field: 'CODE', title: '编号', width: 135 },
              { field: 'KEY_DEVICECODE', title: '点值', width: 135 },
              { field: 'NAME', title: '计量器具名称', width: 135 },
              //{ fixed: '', title: '操作', toolbar: '#equipment-table-tool', width: 80 }
            ]]
          },
          done: function (elem, data) {
            //选择完后的回调，包含2个返回值 elem:返回之前input对象；data:表格返回的选中的数据 []
            //拿到data[]后 就按照业务需求做想做的事情啦~比如加个隐藏域放ID...
            $('#EQUIPMENTNAME').val(data.data[0].NAME);
            $('#EQUIPMENTID').val(data.data[0].ID);
          }
        })
      });
    },

    /** 查询理财产品定义列表 */
    getList() {
      this.loading = true;
      listFinanceProducts(this.queryParams).then(response => {
        this.financeProductsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        financeProductsId: null,
        financeProductsName: null,
        financeType: null,
        breakEvenFlag: "0",
        fixedFlag: null,
        bpId: null,
        bankAccountId: null,
        annualYield: null,
        yieldDescription: null,
        enableFlag: "0",
        purchaseStatus: "0",
        autoRedemption: null,
        redemptionRule: null,
        fixedTerm: null,
        cooperativeAgency: null,
        documentType: null,
        documentCategory: null,
        businessType: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        refV01: null,
        refV02: null,
        refV03: null,
        refV04: null,
        refV05: null,
        refV06: null,
        refV07: null,
        refV08: null,
        refV09: null,
        refV10: null,
        refV11: null,
        refV12: null,
        refV13: null,
        refV14: null,
        refV15: null,
        refN01: null,
        refN02: null,
        refN03: null,
        refN04: null,
        refN05: null,
        refN06: null,
        refN07: null,
        refN08: null,
        refN09: null,
        refN10: null,
        refD01: null,
        refD02: null,
        refD03: null,
        refD04: null,
        refD05: null,
        refD06: null,
        refD07: null,
        refD08: null,
        refD09: null,
        refD10: null,
        historyFlag: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.financeProductsId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加理财产品定义";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const financeProductsId = row.financeProductsId || this.ids
      getFinanceProducts(financeProductsId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改理财产品定义";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.financeProductsId != null) {
            updateFinanceProducts(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addFinanceProducts(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const financeProductsIds = row.financeProductsId || this.ids;
      this.$modal.confirm('是否确认删除理财产品定义编号为"' + financeProductsIds + '"的数据项？').then(function () {
        return delFinanceProducts(financeProductsIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('loan/financeProducts/export', {
        ...this.queryParams
      }, `financeProducts_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
